package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private CategoryDao categoryDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params,String type,Long catelogId) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();
        if (type.equals("sale")){
            w.eq("attr_type",0);
        }else {
            w.eq("attr_type",1);
        }
        if(!catelogId.equals(0l)){
            w.eq("catelog_id",catelogId);
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                w
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> attrRelationByGroupId(Long attrGroupId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("attr_type",1);

        wrapper.eq("attr_group_id",attrGroupId);


        return baseMapper.selectList(wrapper);
    }

    @Override
    public AttrEntity getById(Serializable id) {

        AttrEntity byId = super.getById(id);

        Long catelogId = byId.getCatelogId();
        List<Long> longs = new ArrayList<>();
        getLogPath(longs,catelogId);

        Collections.reverse(longs);
        Long[] ids = longs.toArray(new Long[longs.size()]);

        byId.setCatelogPath(ids);

        return byId;
    }

    private void getLogPath(List<Long> list,Long catelogId){
        list.add(catelogId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if(!categoryEntity.getParentCid().equals(0l))
            getLogPath(list,categoryEntity.getParentCid());
    }

    @Override
    public PageUtils noattrRelation(Map<String, Object> params) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();

        w.isNull("attr_group_id");

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                w
        );

        return new PageUtils(page);
    }


    @Override
    public boolean save(AttrEntity entity) {

        if (entity.getAttrGroupId()!=null){
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(entity.getAttrGroupId());
            entity.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        }
        if (entity.getCatelogId()!=null){
            CategoryEntity categoryEntity = categoryDao.selectById(entity.getCatelogId());
            entity.setCatelogName(categoryEntity.getName());
        }


        return super.save(entity);
    }
}